#include "adddeleteinfo.h"
#include "ui_adddeleteinfo.h"
#include "databaseoperate.h"
addDeleteInfo::addDeleteInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addDeleteInfo)
{
    ui->setupUi(this);
    aDialog = new addDialog;
    connect(aDialog, SIGNAL(sendText(QString)), this, SLOT(addInfo(QString)));
    connect(aDialog, SIGNAL(addNow()), this, SLOT(addNow()));
    initInfo();
    this->setWindowModality(Qt::ApplicationModal);
    this->setWindowTitle("设置");
}

addDeleteInfo::~addDeleteInfo()
{
    delete ui;
}
void addDeleteInfo::initInfo()
{
    ui->listWidget->clear();
    ui->listWidget_2->clear();
    ui->listWidget_3->clear();
    QMap<int, QString> c;
    databaseOperate::Instance()->queryShifts(c);
    QMapIterator<int, QString> i(c);
    while(i.hasNext())
    {
        i.next();
        QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
        item->setText(i.value());
        item->setData(Qt::UserRole, i.key());
        ui->listWidget->addItem(item);
    }
    c.clear();
    databaseOperate::Instance()->querySurveyor(c);
    QMapIterator<int, QString> j(c);
    while(j.hasNext())
    {
        j.next();
        QListWidgetItem *item = new QListWidgetItem(ui->listWidget_2);
        item->setText(j.value());
        item->setData(Qt::UserRole, j.key());
        ui->listWidget_2->addItem(item);
    }
    c.clear();
    databaseOperate::Instance()->querySample(c);
    QMapIterator<int, QString> k(c);
    while(k.hasNext())
    {
        k.next();
        QListWidgetItem *item = new QListWidgetItem(ui->listWidget_3);
        item->setText(k.value());
        item->setData(Qt::UserRole, k.key());
        ui->listWidget_3->addItem(item);
    }
}

void addDeleteInfo::on_pushButton_clicked()
{
    whichList = 1;
    aDialog->show();
}

void addDeleteInfo::addInfo(QString arg)
{
    str = arg;

}
void addDeleteInfo::addNow()
{
    if(str == "" || str.trimmed() == "")
        return;
    switch (whichList) {
    case 1:
        databaseOperate::Instance()->addShifts(str);
        break;
    case 2:
        databaseOperate::Instance()->addSurveyor(str);
        break;
    case 3:
        databaseOperate::Instance()->addSample(str);
        break;
    default:
        break;
    }
    initInfo();
    emit init();
}

void addDeleteInfo::on_pushButton_3_clicked()
{
    whichList = 2;
    aDialog->show();
}

void addDeleteInfo::on_pushButton_5_clicked()
{
    whichList = 3;
    aDialog->show();
}

void addDeleteInfo::on_pushButton_2_clicked()
{
    if(!ui->listWidget->currentItem())
        return;
    int num = ui->listWidget->currentItem()->data(Qt::UserRole).toInt();
    databaseOperate::Instance()->deleteShifts(num);
    initInfo();
    emit init();
}

void addDeleteInfo::on_pushButton_4_clicked()
{
    if(!ui->listWidget_2->currentItem())
        return;
    int num = ui->listWidget_2->currentItem()->data(Qt::UserRole).toInt();
    databaseOperate::Instance()->deleteSurveyor(num);
    initInfo();
    emit init();
}

void addDeleteInfo::on_pushButton_6_clicked()
{
    if(!ui->listWidget_3->currentItem())
        return;
    int num = ui->listWidget_3->currentItem()->data(Qt::UserRole).toInt();
    databaseOperate::Instance()->deleteSample(num);
    initInfo();
    emit init();
}
